#!/usr/bin/python
#-*- coding: utf-8 -*-

from pysqlite2 import dbapi2 as sqlite

class DB:
    ''' '''
    def __init__(self):
        ''' '''
        pass
    def getCur(self, con):
        ''' '''
        #try:
        if con == '':
            #con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES)
            #self.con = sqlite.connect("db.db", detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES)
            self.con = sqlite.connect("sqldb", detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES) # create connection
        else:
            self.con = sqlite.connect(con, detect_types=sqlite.PARSE_DECLTYPES|sqlite.PARSE_COLNAMES) # create connection

        #http://www.py.cz/Pysqlite
        self.con.text_factory = str

        #except:
        #    print "I am unable to connect to the database"
        self.cur = self.con.cursor()

    def exeQry(self, q):
        self.cur.execute(q)
        self.con.commit()               #
        #row = ''
        return self.cur.fetchall()
        '''
        while row <> None:
            row = self.cur.fetchone() # cur.fetchall() or cur.fetchone()
            return row
        '''
if __name__ == "__main__":
    ''' '''
    #!/usr/bin/python
    # -*- coding: cp1250 -*-
    #import pepe.p_sql.qry_sqlite as q
    
    x = DB()
    x.getCur('sqltdb')
    #x.getCur(':memory:')
    if 0:
        q = '''create table ta (c001 char, c002 char)'''
        for row in x.exeQry(q):
            print row[1]
        q = '''insert into ta (c001, c002) values ('a', '1')'''
        for row in x.exeQry(q):
            print row[1]
    if 1:
        q = '''select * from ta where c001 in ('a', 'b')'''
        for row in x.exeQry(q):
            print row[1]
        
